<script>
//需要导入什么：reactive 把数据变成响应式数据    toRefs：把数据展开，达到写代码更简洁的目的
import { reactive, toRefs } from "vue";
// 还要导入什么：axios  达到前后端交互的目的
import axios from "axios";
//导入router  路由器
import { useRouter } from "vue-router";

export default { //前端js代码写法使用组合式APIsetup(){
    setup() {

        const router = useRouter();

        const state = reactive({
            users: {
                userId: "",
                password: "",
                realName: "",
                sex: '1',
                identityCard: "",
                birthday: "",
                userType: "1"
            },
            password1: ""
        })
        //身份证的正则表达校验
        function checkIdCard(idCard) {
            var reg = /^(\d{15}$|^\d{18}$|^\d{17}(\d|X|x))$/;
            return reg.test(idCard);
        }
        //手机的正则表达校验
        function checkPhoneNumber(phoneNumber) {
            var reg = /^1[3-9]\d{9}$/;
            return reg.test(phoneNumber);
        }

        function register() {

            if (state.users.userId == '') {
                alert('手机号码不能为空')
                return;
            }
            if (!checkPhoneNumber(state.users.userId)) {
                alert('手机号码格式不正确');
                return;
            }
            if (state.users.realName == '') {
                alert('姓名不能为空')
                return;
            }
            if (state.users.birthday == '') {
                alert('生日不能为空')
                return;
            }
            if (state.users.identityCard == '') {
                alert('身份证不能为空')
                return;
            }
            if (!checkIdCard(state.users.identityCard)) {
                alert('身份证号码格式不正确');
                return;
            }
            if (state.users.password == '') {
                alert('密码不能为空')
                return;
            }
            if (state.users.password1 != state.users.password) {
                alert('两次密码不相同')
                return;
            }

            //都不为空，那么代码继续往下走
            axios.post('api/users/register', state.users).then((response) => {
                // console.log(response.data.birthday)
                let u = response.data;
                if (u.status == 200) {
                    router.push('/');
                    alert("注册成功")
                } else {
                    alert(u.desc);
                    return state.users.userId = '';
                }
            }).catch((error) => {
                console.log(error)
            })
        }
        return {
            ...toRefs(state),
            register
        }

    }
}
</script>



<template>

    <!-- 总容器 -->
    <div class="wrapper">
        <header>
            <i class="fa fa-angle-left" onclick="history.go(-1)"></i>
            <p>注册</p>
            <div></div>
        </header>
        <div class="top-ban"></div>
        <h1>欢迎注册</h1>
        <table>
            <tr>
                <td>手机号码</td>
                <td><input type="text" placeholder="请输入手机号码" v-model="users.userId"></td>
            </tr>
            <tr>
                <td>真实姓名</td>
                <td><input type="text" placeholder="真实姓名便于查看体检报告" v-model="users.realName"></td>
            </tr>
            <tr>
                <td>生日</td>
                <td><input type="date" v-model="users.birthday"></td>
            </tr>
            <tr>
                <td>性别</td>
                <td>
                    <input type="radio" checked v-model="users.sex" value="1">男
                    <input type="radio" v-model="users.sex" value="0">女
                </td>
            </tr>
            <tr>
                <td>身份证号</td>
                <td><input type="text" placeholder="请输入身份证号" v-model="users.identityCard"></td>
            </tr>
            <tr>
                <td>密码</td>
                <td><input type="password" placeholder="请输入密码" v-model="users.password"></td>
            </tr>
            <tr>
                <td>确认密码</td>
                <td><input type="password" placeholder="请再次输入密码" v-model="users.password1"></td>
            </tr>
        </table>
        <div class="btn" @click="register">完成</div>
    </div>

</template>



<style scoped>
/*********************** 总容器 ***********************/
.wrapper {
    width: 100%;
    height: 100%;
}

/*********************** header ***********************/
header {
    width: 100%;
    height: 15.7vw;
    background-color: #FFF;

    position: fixed;
    left: 0;
    top: 0;

    display: flex;
    align-items: center;
    justify-content: space-between;

    box-sizing: border-box;
    padding: 0 3.6vw;
}

header .fa {
    font-size: 8vw;
}

/*********************** 标题部分 ***********************/
h1 {
    font-size: 7.4vw;
    font-weight: 500;
    box-sizing: border-box;
    padding: 0 3.6vw;
    margin-top: 6vw;
}

/*********************** common样式 ***********************/
.top-ban {
    width: 100%;
    height: 15.7vw;
}

/*********************** table ***********************/
table {
    width: 92.8vw;
    margin: 0 auto;
    margin-top: 5vw;
    border-collapse: collapse;

    font-size: 4.2vw;
}

table tr td {
    height: 12vw;
    border-bottom: solid 1px #DDD;
}

table tr td input {
    border: none;
    outline: none;
}

/*********************** btn ***********************/
.btn {
    width: 92.8vw;
    margin: 0 auto;
    height: 12vw;
    margin-top: 8vw;
    background-color: #137E92;
    border-radius: 2vw;
    color: #FFF;
    font-size: 5vw;
    text-align: center;
    line-height: 12vw;

    user-select: none;
    cursor: pointer;
}
</style>